import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import legacy from "@vitejs/plugin-legacy";
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import {
  ElementPlusResolver,
  VantResolver
} from "unplugin-vue-components/resolvers";
import { viteExternalsPlugin } from 'vite-plugin-externals'

export default defineConfig(({mode}) => {
  const config = loadEnv(mode, './');
  
  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver(), VantResolver()], // 按需引入
      }),
      Components({
        resolvers: [ElementPlusResolver(), VantResolver()],
      }),
      legacy({
        targets: ["ie >= 11"],
        additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
      }),
      viteExternalsPlugin({
        // Kakao: 'Kakao',
        // 'naver_id_login': 'naver_id_login'

        // vue: 'Vue',
        // react: 'React',
        // 'react-dom': 'ReactDOM',
        // // 支持值链式取值，会转换成 window['React']['lazy']
        // lazy: ['React', 'lazy']
      }),
    ],
    server: {
      proxy: {
        '/api': {
          target: config.VITE_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
      // https: true,
    },
    resolve: {
      alias: {
        "@/": `${path.resolve(__dirname, "src")}/`,
        vue: "vue/dist/vue.esm-bundler.js",
      },
    }
  }
});
